package com.cn.transfer.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.YY1_KPI;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.mapper.Yy1KpiMapper;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: weidian
 * @date: 2024/10/10 14:27
 * @Description: 一次性使用的接口
 */
@RestController
public class OneTimeController {

    @Autowired
    private Yy1KpiMapper yy1KpiMapper;

    @GetMapping("/KPL")
    public void YY_KPl(){
        Header[] headers = new Header[3];
        headers[0] = new BasicHeader("Accept", "application/json");
        headers[1] = new BasicHeader("Authorization", "Basic " + KeyConstant.SAP01);
        headers[2] = new BasicHeader("x-csrf-token", "Fetch");
        JSONObject entries = ApiClient.sendGetRequest("https://my200654-api.s4hana.sapcloud.cn/sap/opu/odata/sap/YY1_KPI_CDS/YY1_KPI", headers, null);
        String d = entries.getStr("d");
        JSONObject objects = JSONUtil.parseObj(d);
        String results = objects.getStr("results");
        JSONArray objectss = JSONUtil.parseArray(results);
        List<YY1_KPI> yy1Kpis = JSONUtil.toList(objectss, YY1_KPI.class);
        for (YY1_KPI yy1Kpi : yy1Kpis) {
            if (yy1Kpi.getSalesDocumentDate().isEmpty()){
                long lct = Long.parseLong(ConnmeUtils.extractTimestamp(yy1Kpi.getSalesDocumentDate()));
                LocalDateTime dateTime2 = ConnmeUtils.convertTimestampToLocalDateTime(lct);
                String formattedDateTime2 = ConnmeUtils.formatDateTime(dateTime2);
                DateTime dateTime4 = new DateTime(formattedDateTime2, DatePattern.NORM_DATETIME_FORMAT);
                yy1Kpi.setSalesDocumentDates(dateTime4);
            }
            if (yy1Kpi.getLastTimesTamp().isEmpty()){
                long lct = Long.parseLong(ConnmeUtils.extractTimestamp(yy1Kpi.getLastTimesTamp()));
                LocalDateTime dateTime2 = ConnmeUtils.convertTimestampToLocalDateTime(lct);
                String formattedDateTime2 = ConnmeUtils.formatDateTime(dateTime2);
                DateTime dateTime4 = new DateTime(formattedDateTime2, DatePattern.NORM_DATETIME_FORMAT);
                yy1Kpi.setLastTimesTamps(dateTime4);
            }
            if (yy1Kpi.getRateStartDate().isEmpty()){
                long lct = Long.parseLong(ConnmeUtils.extractTimestamp(yy1Kpi.getRateStartDate()));
                LocalDateTime dateTime2 = ConnmeUtils.convertTimestampToLocalDateTime(lct);
                String formattedDateTime2 = ConnmeUtils.formatDateTime(dateTime2);
                DateTime dateTime4 = new DateTime(formattedDateTime2, DatePattern.NORM_DATETIME_FORMAT);
                yy1Kpi.setRateStartDates(dateTime4);
            }
            yy1KpiMapper.insertBatch(yy1Kpi);
        }

    }
}
